package com.example.demo.controller;

import com.example.demo.common.AjaxResult;
import com.example.demo.entity.Userinfo;
import com.example.demo.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @RequestMapping("/login")
    public AjaxResult login( String username, String password) {
        // 2.查询数据库
        Userinfo userinfo = userService.getUserByName(username);

        if (userinfo != null && userinfo.getId() > 0) {   // 有效的用户
            // 两个密码是否相同
            if (password.equals(userinfo.getPassword())) {
                userinfo.setPassword(""); // 返回前端之前，隐藏敏感（密码）信息
                return AjaxResult.success(userinfo);
            }
        }
        return AjaxResult.success(0, null);

    }

}
